//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.ServiceBus.Samples
{
    using System;
    using System.ServiceModel.Channels;

    using Microsoft.ServiceBus;

    internal class Producer
    {
        private MessageBufferClient client;
        private TransportClientEndpointBehavior credential;
        private Uri uri;

        public Producer()
        {
            Console.Write("Your Service Namespace: ");
            string serviceNamespace = Console.ReadLine();
            Console.Write("Your Issuer Name: ");
            string issuerName = Console.ReadLine();
            Console.Write("Your Issuer Secret: ");
            string issuerSecret = Console.ReadLine();

            // create the credentials object for the endpoint
            this.credential = new TransportClientEndpointBehavior();
            this.credential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

            // create the URI for the message buffer
            this.uri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "MessageBuffer");
        }

        public void ProduceMessages()
        {
            Console.Write("Press [Enter] to connect to the message buffer: ");
            Console.ReadLine();

            // create the client for the message buffer
            this.client = MessageBufferClient.GetMessageBuffer(this.credential, this.uri);
            Console.WriteLine("Connected to the message buffer at '{0}'.", this.client.MessageBufferUri);

            Console.Write("Press [Enter] to send messages to the message buffer: ");
            Console.ReadLine();

            // send three messages to the message buffer
            for (int i = 1; i <= 3; ++i)
            {
                Message message = Message.CreateMessage(MessageVersion.Default, string.Empty, string.Format("<<MESSAGE {0}>>", i));
                this.client.Send(message);
                Console.WriteLine("Sent message with body '<<MESSAGE {0}>>' to the message buffer.", i);
                message.Close();
            }

            Console.WriteLine("Press any key to continue . . .");
            Console.ReadKey();
        }
    }
}
